/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-11-04
 * Time: 21:17
 */

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
//递归反转链表
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode newHead=reverseList(head.next);
        //反转节点
        head.next.next=head;
        //不置空会形成环
        head.next=null;
        //newHead的作用就是返回最后一个节点即新的头节点
        return newHead;
    }
}
